package com.hk.yajin.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLayoutChangeListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hk.developmentlib.R;
import com.hk.developmentlib.base.DevelopmentKit;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.developmentlib.view.photozoom.ExtendedViewPager;
import com.hk.developmentlib.view.photozoom.TouchImageView;


/**
 * @author wl 图片缩放
 */
public class PhotoZoomActivity extends Activity {

	/* private ImageView img_zoom; */

    private ExtendedViewPager mViewPager;
    private TextView txt_index;

    private static String[] images;
    private int mPosition;
    private boolean isLocalImg = false;
    @SuppressWarnings("unused")
    private String address;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_photo_zoom);
        mViewPager = (ExtendedViewPager) findViewById(R.id.view_pager);
        txt_index = (TextView) findViewById(R.id.txt_index);
        Intent intent = getIntent();
        if (intent.getExtras() != null) {
            if (intent.getStringArrayExtra("imageUrls") != null) {
                images = intent.getStringArrayExtra("imageUrls");
            }
            mPosition = intent.getIntExtra("position", 0);
            isLocalImg = intent.getBooleanExtra("localImg", false);
            address = intent.getStringExtra("address");
        }
        mViewPager.setAdapter(new TouchImageAdapter());
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setCurrentItem(mPosition);
        mViewPager.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
//        mViewPager.addOnLayoutChangeListener(new OnLayoutChangeListener() {
//
//            @Override
//            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
//                                       int oldRight, int oldBottom) {
//                txt_index.setText((mViewPager.getCurrentItem() + 1) + "/" + images.length);
//                txt_index.setVisibility(View.VISIBLE);
//                txt_index.removeCallbacks(runnable);
//                txt_index.postDelayed(runnable, 4000);
//            }
//        });

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                txt_index.setText((mViewPager.getCurrentItem() + 1) + "/" + images.length);
                txt_index.setVisibility(View.VISIBLE);
                txt_index.removeCallbacks(runnable);
                txt_index.postDelayed(runnable, 4000);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            if (txt_index != null) {
                txt_index.setVisibility(View.GONE);
            }
        }
    };

    class TouchImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            if (images != null && images.length > 0) {
                return images.length;
            } else {
                return 0;
            }

        }

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            TouchImageView img = new TouchImageView(container.getContext());
            String imgUrl = null;
            if (images[position] != null) {
                if (!isLocalImg) {
                    //imgUrl = URLUtils.getDownUrl(images[position], address);
                    imgUrl = images[position].toString();
                } else {
                    imgUrl = images[position];
                }
            }
            //Glide.with(DevelopmentKit.getContext()).load(imgUrl).placeholder(R.color.oriag2).error(R.color.app_line_eaeaea).into(img);

            UiUtil.showNormalImage(img,imgUrl);

            container.addView(img, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            img.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    PhotoZoomActivity.this.finish();
                }
            });
            return img;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

    }


}
